package com.ljw.miniweb.board.controller;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.ljw.miniweb.board.bean.BoardBean;
import com.ljw.miniweb.board.dao.BoardDao;


/**
name    : BoardController
content : 게시판 controller
date    : 2011.02.20
author  : ljw

@author ljw(jwlee1728@gmail.com)
@version 1.0

* @see class_name : "See also"라는 항목을 만들어 해당 클래스와 연결해준다.
* @see class_name#method_name : "See also"라는 항목을 만들어 특정 메소드와 연결해준다.
* @param name description : 특정 메소드가 취하는 파라메터를 기술할 때 사용된다.
* @return description : 특정 메소드의 리턴값을 기술할 때 사용된다.
* @exception class_name : 특정 메소드가 발생시킬 수 있는 에외상황을 기술할 때 사용된다.
*/
@Controller
@RequestMapping(value="/board.ljw")
public class BoardController {
	protected final Log logger = LogFactory.getLog(this.getClass());
	
	@Resource(name="boardDao")
	private BoardDao boardDao;
	
	/*public void setBoardDao(BoardDao boardDao){
		this.boardDao = boardDao;
	}*/
	
	/** 
	 * Method   : list
	 * 최초작성일  : 2011. 11. 3. 
	 * 작성자 : jw
	 * 변경이력 : 
	 * @param request
	 * @param response
	 * @param command
	 * @return 
	 * Method 설명 : 게시판 리스트 조회  
	 */
	@RequestMapping(params="method=list")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response, Object command){
		logger.info("BoardController.list()");
		ArrayList list = new ArrayList();
		
		try {
			list = boardDao.getBoardList();
			
		} catch (SQLException e) {
			BoardBean boardBean = new BoardBean();
			list = (ArrayList) boardBean.getDummy();
		}
		
		//ModelAndView modelAndView = new ModelAndView("jsp/board/boardList");
		ModelAndView modelAndView = new ModelAndView("/WEB-INF/jsp/board/boardList.jsp");
		modelAndView.addObject("list", list);
		
		return modelAndView;
	}
	
	
	/** 
	 * Method   : write
	 * 최초작성일  : 2011. 11. 3. 
	 * 작성자 : jw
	 * 변경이력 : 
	 * @param request
	 * @param response
	 * @param command
	 * @return 
	 * Method 설명 : 게시판에서 글 등록시 호출 
	 */
	public ModelAndView write(HttpServletRequest request, HttpServletResponse response, Object command){
		logger.info("BoardController.write()");
		logger.info("content = " + (String)request.getParameter("content"));
			BoardBean boardBean = new BoardBean();
			ArrayList list = new ArrayList();
			
		try{
			// bind(request, boardBean);
			 boardDao.insertBoard(boardBean);
			 list = boardDao.getBoardList();
		}catch(SQLException e){
			//실패시 dummy 자료를 보여준다
			boardBean = new BoardBean();
			list = (ArrayList) boardBean.getDummy();
		}catch(Exception e){
			//실패시 dummy 자료를 보여준다
			boardBean = new BoardBean();
			list = (ArrayList) boardBean.getDummy();
		}
		
		ModelAndView modelAndView = new ModelAndView("jsp/board/boardListInner");
		modelAndView.addObject("list", list);
			
		return modelAndView;
	}
}
